package com.industrial.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.industrial.common.core.domain.R;
import com.industrial.common.core.utils.MD5Utils;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.utils.bean.BeanUtils;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.system.api.domain.BizAccountMapping;
import com.industrial.system.api.domain.SysUser;
import com.industrial.system.api.model.LoginUser;
import com.industrial.system.mapper.SysUserMapper;
import com.industrial.system.service.IBizAccountMappingService;
import com.industrial.system.service.ISysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * 山东省认证平台认证远程调用
 */
@RestController
@RequestMapping("/cnm")
public class BizAccountMappingController {

    private IBizAccountMappingService bizAccountMappingService;

    private SysUserMapper sysUserMapper;

    private ISysPermissionService permissionService;

    public BizAccountMappingController(IBizAccountMappingService bizAccountMappingService,SysUserMapper sysUserMapper,ISysPermissionService permissionService) {
        this.bizAccountMappingService = bizAccountMappingService;
        this.sysUserMapper = sysUserMapper;
        this.permissionService = permissionService;
    }

    /**
     * 通过省用户Id查询省用户与业务用户的关系
     * @param userInfoMap 省用户信息
     * @return
     */
    @GetMapping("/selectUserMappingInfoByZwfwuuid")
    public R<LoginUser> selectUserMappingInfoByZwfwuuid(@RequestParam Map<String,Object> userInfoMap){
        try {
            LoginUser bizAccountMapping = bizAccountMappingService.selectUserMappingInfoByZwfwuuid(userInfoMap);
            return R.ok(bizAccountMapping);
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
    }

    /**
     * 通过省用户的ID查询当前关联业务用户数据
     * @param uuid 省法人用户的Id
     * @return
     */
    @GetMapping("/cnnnnn/{uuid}/{uscc}")
    public R<Map<String,Object>> querySUserToPtUserMapping(@PathVariable("uuid")String uuid,@PathVariable("uscc") String uscc){
       try{
           BizAccountMapping bizAccountMapping = bizAccountMappingService.selectUserInfoBySptUuid(uuid,uscc);
           return R.ok(BeanUtil.beanToMap(bizAccountMapping));
       }catch (Exception e){
           e.printStackTrace();
            return R.fail(e.getMessage());
       }
    }


    //山东通登陆需要的远程接口

    /**
     * 查询山东通用户和业务用户的绑定状态
     * @param userId 山东通用户ID
     * @return
     */
    @GetMapping("/querySdtUserBingBizUserStatus/{sUserId}")
    public R<LoginUser> querySdtUserBingBizUserStatus(@PathVariable("sUserId") String userId){
        LoginUser loginUser = bizAccountMappingService.selectUserInfoBySptId(userId);
        return R.ok(loginUser);
    }

    @PostMapping("/sdtUserBindBizUser")
    public R<?> sdtUserBindBizUser(@RequestBody BizAccountMapping sysUser){
        if(StringUtils.isBlank(sysUser.getBizUserName()) || StringUtils.isBlank(sysUser.getBizUserPwd()) || StringUtils.isBlank(sysUser.getsPtUserId())){
            return R.fail("用户或密码错误");
        }
        String bizUserName = sysUser.getBizUserName();
        String bizUserPwd = sysUser.getBizUserPwd();
        String sPtUserId = sysUser.getsPtUserId();

        //校验用户名和密码
        SysUser dbSysUser = sysUserMapper.selectUserByUserName(bizUserName);
        if(dbSysUser == null){
            return R.fail("没有找到该用户");
        }
        if(StringUtils.equals(MD5Utils.encrypt(bizUserName,bizUserPwd),dbSysUser.getPassword())){

            BizAccountMapping dbBizAccountMapping = bizAccountMappingService.selectUserBindBySptId(sPtUserId);
            if(dbBizAccountMapping != null){
                return R.fail("该用户已经绑定其它山东通账号");
            }

            //绑定用户关系
            BizAccountMapping bizAccountMapping = new BizAccountMapping();
            bizAccountMapping.setBizUserId(dbSysUser.getUserId());
            bizAccountMapping.setBizUserName(bizUserName);
            bizAccountMapping.setBizUserPwd(dbSysUser.getPassword());
            bizAccountMapping.setsPtUserId(sPtUserId);
            bizAccountMapping.setId(IdUtil.simpleUUID());
            bizAccountMappingService.insert(bizAccountMapping);

            LoginUser loginUser = new LoginUser();
            loginUser.setSysUser(dbSysUser);
            // 角色集合
            Set<String> roles = permissionService.getRolePermission(dbSysUser);
            // 权限集合
            Set<String> permissions = permissionService.getMenuPermission(dbSysUser);
            loginUser.setRoles(roles);
            loginUser.setPermissions(permissions);
            return R.ok(loginUser);
        }else{
            return R.fail("用户名或密码不正确");
        }
    }
}
